<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *智能体
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'设备智能体'),
	array('op'=>'list1','title'=>'自定义角色'),
	//array('op'=>'listadd','title'=>'批量导入智能体表'),
);
if($_RQ['op']=='list' || $_RQ['op']=='list1'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['op']=='list1'){
		$params['status']=1;
	}else{
		$params['status']=2;
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(!empty($_RQ['agetype'])){
		$params['agetype']=intval($_RQ['agetype']);
		$return['params']['agetype']=$_RQ['agetype'];
	}
	if(!empty($_RQ['yintype'])){
		$params['yintype like']='%'.trim($_RQ['yintype']).'%';
		$return['params']['yintype']=$_RQ['yintype'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_aiagent_useragent',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['voice']=pdo_get('jy_aiagent_voice',array('id'=>$item['voiceid'],'uniacid'=>$_SESSION['uniacid']));
			$item['language']=$item['voice']['language'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$tips='这里是平台智能体明白，用户的智能体不在这里展示！';
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tips,'edit'=>false,'del_one'=>true,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'title','title'=>'智能体名称','type'=>'string'),
			array('name'=>'apiid','title'=>'APIid','type'=>'string'),
			array('name'=>'language','title'=>'语言','type'=>'radio','options'=>'yuyuantypes'),
			array('name'=>'voiceid','title'=>'绑定音色','type'=>'select','options'=>'voices'),
			array('name'=>'default_tts_voice','title'=>'默认voice参数值','type'=>'string'),
			//array('name'=>'groupid','title'=>'智能体分类','type'=>'select','options'=>'groups'),
			array('name'=>'desc','title'=>'描述','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'getchat','title'=>'同步对话'),
			array('type'=>'href','action'=>'/useragentlog/','title'=>'历史对话'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'智能体名称','type'=>'string'),
		)
	);
	$return['agetype']=$this->agetype;
	$return['yuyuantypes']=$this->yuyuantypes;
	$return['voices']=pdo_getall('jy_aiagent_voice',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	//$return['groups']=pdo_getall('jy_aiagent_agent_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	$return['isfen']=array('不支持','支持');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aiagent_useragent',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
		$item['voice']=pdo_get('jy_aiagent_voice',array('id'=>$item['voiceid'],'uniacid'=>$_SESSION['uniacid']));
		$item['language']=$item['voice']['language'];	
	}else{
		$item=array('type'=>$this->session('type'),'star'=>3,'isnews'=>0);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回智能体列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'智能体名称','type'=>'string'),
			array('name'=>'logo','title'=>'智能体头像','type'=>'image'),
			array('name'=>'ainame','title'=>'AI机器人名称','type'=>'string'),
			array('name'=>'user_name','title'=>'主人名称','type'=>'string'),
			array('type'=>'select2_line','name'=>'language','options'=>'yuyuantypes','title'=>'语言','help'=>'',
				'name2'=>'voiceid','options2'=>'voices','title2'=>'音色',
			),
			array('name'=>'groupid','title'=>'主分类','type'=>'select','options'=>'groups'),
			array('name'=>'llm_model','title'=>'语言模型','type'=>'radio','options'=>'llm_models'),
			array('name'=>'desc','title'=>'智能体介绍','type'=>'text'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['llm_models']=$this->llm_models;
	//$return['groups']=pdo_getall('jy_aiagent_agent_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$yuyuantypes=$this->yuyuantypes;
	$return['yuyuantypes']=array();
	foreach($yuyuantypes as $k=>$title){
		$return['yuyuantypes'][$k]=array('id'=>$k,'title'=>$title);
	}
	foreach($return['yuyuantypes'] as $k=>&$yv){
		$yv['voices']=pdo_getall('jy_aiagent_voice',array('uniacid'=>$_SESSION['uniacid'],'language'=>$yv['id']),array('title','id'),'id');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['mid']){
		//exi('请选择发布人！','error');
	}
	if(!$postdata['title']){
		exi('请填写智能体名称！','error');
	}
	if(!$postdata['groupid']){
		exi('请填写智能体分类！','error');
	}
	if(!$postdata['voiceid']){
		exi('请绑定音色！','error');
	}
	if(!$postdata['llm_model']){
		exi('请绑定语言模型！','error');
	}
	$voice=pdo_get('jy_aiagent_voice',array('id'=>$postdata['voiceid'],'uniacid'=>$_SESSION['uniacid']));
	$data=array(
		'title'=>trim($postdata['title']),
		'logo'=>trim($postdata['logo']),
		'desc'=>trim($postdata['desc']),
		'default_tts_voice'=>$voice['language'].':'.$voice['voiceid'],
		'voiceid'=>$postdata['voiceid'],
		'llm_model'=>trim($postdata['llm_model']),
		'ainame'=>trim($postdata['ainame']),
		'user_name'=>trim($postdata['user_name']),
		'groupid'=>intval($postdata['groupid']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
	);
	//$res=$this->model('api')->upagentstpl($data,1);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_aiagent_useragent',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$data['apiid']=$res['id'];
		pdo_insert('jy_aiagent_useragent',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择智能体！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_aiagent_useragent',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='updata'){
	$post=array(
		'page'=>1,
		'pageSize'=>100,
	);
	$isvoice=pdo_count('jy_aiagent_voice',array('uniacid'=>$_SESSION['uniacid']));
	if($isvoice<3){
		exi('请先同步音色！至少3种以上！','error');
	}
	$list=$this->model('api')->getagentstpl($post,1);
	foreach($list as $item){
		$isadd=pdo_get('jy_aiagent_useragent',array('apiid'=>trim($item['id']),'uniacid'=>$_SESSION['uniacid']));
		$default_tts_voice=str_array($item['default_tts_voice'],':');
		$voice=pdo_get('jy_aiagent_voice',array('language'=>$default_tts_voice[0],'voiceid'=>trim($default_tts_voice[1]),'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'apiid'=>trim($item['id']),
			'title'=>trim($item['agent_name']),
			'logo'=>trim($item['logo']),
			'desc'=>trim($item['character']),
			'tts_voices'=>ijson_encode($item['tts_voices']),
			'default_tts_voice'=>trim($item['default_tts_voice']),
			'voiceid'=>$voice['id'],
			'llm_model'=>trim($item['llm_model']),
			'ainame'=>trim($item['assistant_name']),
			'user_name'=>trim($item['user_name']),
			'tts_speech_speed'=>trim($item['tts_speech_speed']),
			'asr_speed'=>trim($item['asr_speed']),
			'tts_pitch'=>trim($item['tts_pitch']),
			'tts_voice_name'=>trim($item['tts_voice_name']),
			'status'=>1,
		);
		$id=intval($isadd['id']);
		if($id){
			pdo_update('jy_aiagent_useragent',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_aiagent_useragent',$data);
		}
	}
	exi('同步成功！');
}
if($_RQ['op']=='updemo'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择智能体','error');
	}
	if(!$this->setting['zhubodemo']){
		exi('请先设置【试听文字】','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			$zhubo=pdo_get('jy_aiagent_useragent',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			if(!$zhubo['voice']){
				continue;
			}
			$post=array(
				'content'=>$this->setting['zhubodemo'],
				'voice'=>$zhubo['voice'],
				'volume'=>'1.0',
				'rate'=>'1.0',
				'pitch'=>'1.0',
			);
			$mp3=$this->model('api')->upmp3($post);
			if($mp3){
				pdo_update('jy_aiagent_useragent',array('demo'=>$mp3),array('id'=>$zhubo['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='getchat'){
	$id=$_RQ['id'];
	$agent=pdo_get('jy_aiagent_useragent',array('id'=>trim($id),'uniacid'=>$_SESSION['uniacid']));
	if($agent['getchattime']>time()-120){
		exi('请先请勿频繁同步操作','error');
	}
	$this->model('agent')->getchat($agent,1);
	exi('同步成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$agent=pdo_get('jy_aiagent_useragent',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$this->model('api')->deluseragent($agent['apiid'],1);
			pdo_delete('jy_aiagent_useragent',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}